<?php

/**
 * SvcPHP
 *
 * Copyright (C) 2012 João Francisco Biondo Trinca
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package    SvcPHP
 * @author     João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @copyright  2012 João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://code.google.com/p/svcphp/
 *
 */

/**
 * Implements the translator class.
 */
class Svc_Translator
{
    /**
     * Stores the current language.
     *
     * @var mixed
     */
    private $_lang = 'pt';

    /**
     * Stores the loaded language data.
     *
     * @var mixed
     */
    private $_data = array();

    /**
     * Creates a translator class instance.
     *
     * @return Svc_Translator
     */
    public function __construct()
    {
        if (Svc_Application::instance()->getCookies()->get('__svc_language__', null) === null)
        {
            Svc_Application::instance()->getCookies()->set('__svc_language__', 'pt', 2678400);
        }
        $this->load(Svc_Application::instance()->getCookies()->get('__svc_language__'));
    }

    /**
     * Loads the messages from a file.
     *
     * @param $lang Language key
     * @return bool
     */
    private function load($lang)
    {
        $file_msg = Svc_Application::instance()->getPath("data/messages.dat");
        if (!file_exists($file_msg))
        {
            return false;
        }

        $hash = sha1_file($file_msg);
        $file_cache = Svc_Application::instance()->getPath("cache/languages/" . $hash . "." . $lang . ".php");

        if (file_exists($file_cache))
        {
            $this->_lang = $lang;
            $this->_data = unserialize(file_get_contents($file_cache));
            return true;
        }

        $parsed = parse_ini_file($file_msg, true);

        $languages = array();
        foreach($parsed as $section => $values)
        {
            foreach($values as $language => $value)
            {
                $languages[$language][$section] = $value;
            }
        }

        foreach ($languages as $language => $content)
        {
            $file = Svc_Application::instance()->getPath("cache/languages/" . $hash . "." . $language . ".php");
            file_put_contents($file, serialize($content));
        }

        if (isset($languages[$lang]))
        {
            $this->_lang = $lang;
            $this->_data = $languages[$lang];
            return true;
        }

        return false;
    }

    /**
     * Sets the current language.
     *
     * @param string $lang
     */
    public function setLanguage($lang)
    {
        if ($this->load($lang))
        {
            Svc_Application::instance()->getCookies()->set('__svc_language__', $lang, 2678400);
        }
    }

    /**
     * Gets the current language.
     *
     * @return string
     */
    public function getLanguage()
    {
        return Svc_Application::instance()->getCookies()->get('__svc_language__', 'en');
    }

    /**
     * Translates a message key.
     *
     * @param string $key
     *
     * @return string
     */
    public function translate($key, $params = array())
    {
        if (isset($this->_data[$key]))
        {
            $message = @vsprintf(@$this->_data[$key], $params);
            if ($message === false)
            {
                $message = @$this->_data[$key];
            }

            return $message;
        }
        else
        {
            return "[[$key]]";
        }
    }

}